<?php
namespace Shop\Controller;
use Common\Controller\AdminbaseController;
class AttradminController extends AdminbaseController{
	private $home;
	private $type;
	private $attr;
	public function _initialize(){
		parent::_initialize();
		$this->home=M('shop_category');
		$this->type=M('shop_type');
		$this->attr=M('shop_attribute');
	}
	function index(){
		$typelist=$this->type->select();
		$this->assign('typelist',$typelist);
		$this->display("index");
	}
	function addType(){
		$this->display("add");
	}
	function addTypePost(){
		$result=$this->type->add($_POST);
		if ($result) {
			echo "增加成功 ".$result;
		} else {
			echo "失败";
		}
		
	}
	function editType(){
		$typelist=$this->type->where("cat_id=".$_GET['cat_id'])->find();
		$this->assign('typelist',$typelist);
		$this->display('edit');
	}
	function editAttr(){
		$typelist=$this->type->select();
		$selectType=$this->type->where('cat_id='.$_GET['cat_id'])->find();
		$attr_group=explode(',',$selectType['attr_group']);
		$attr_group=json_encode($attr_group);
		$this->assign('typelist',$typelist);
		$this->assign('attr_group',$attr_group);
		$attrlist=$this->attr->where("attr_id=".$_GET['attr_id'])->find();
		$attrlist=json_encode($attrlist);
		$this->assign('attrlist',$attrlist);
		$this->display('editAttr');
	}
	function editTypePost(){
		$result=$this->type->where('cat_id='.$_POST['cat_id'])->save($_POST);
		if ($result) {
			echo "修改成功 ".$result;
		} else {
			echo "失败";
		}
		
	}
	function editAttrPost(){
		$result=$this->attr->where('attr_id='.$_POST['attr_id'])->save($_POST);
		if ($result) {
			echo "修改成功 ".$result;
		} else {
			echo "失败";
		}
		
	}
	function deleteType(){
		$delete=$this->type->delete($_GET['cat_id']);	
		$this->index();
	}
	function attrlist(){
		$attrlist=$this->attr->select();
		$typelist=$this->type->select();
		$i=0;
		foreach($typelist as $key=>$type){
			$attrlist[$i]['cat_id']=$type;
			switch($attrlist[$i]['attr_input_type']){
			case 1:
				$attrlist[$i]['attr_input_type']='从列表中选择';
				break;
			case 2:
				$attrlist[$i]['attr_input_type']='多行文本框';
				break;
			default:
				$attrlist[$i]['attr_input_type']='手动输入';
			}
			$i=$i+1;
		}
		print_r($attrlist);
		$this->assign('attrlist',$attrlist);
		$this->display('attrlist');
	}
	function addAttr(){
		$typelist=$this->type->select();
		$this->assign('typelist',$typelist);
		$this->display('addAttr');
	}
	function addAttrPost(){
		$result=$this->attr->add($_POST);
		if($result){
			echo '增加属性成功'.$result;
		}else{
			echo '增加失败';
		}
	}
	function deleteAttr(){
		$delete=$this->attr->delete($_GET['attr_id']);
		$this->attrlist();
	}
	function typeChange(){				//添加属性，选择商品类型后出发函数
		$typelist=$this->type->where("cat_id=".$_POST['cat_id'])->find();
		$attr_group=explode(',',$typelist['attr_group']);
		$show=json_encode($attr_group);
		echo $show;
	}
}
